import { getlogin, getprofile, getUserDetailById } from "../../http/login"
import { getToken, setToken, removeToken } from "../../utils/cokintoken"

const state = {
    token: getToken(),
    userInfo: {}
}

const mutations = {
    // 设置token
    setToken(state, payload) {
        // 设置token
        state.token = payload // 此时认为payload就是传过来的token
        // 应该同步修改本地缓存的数据
        setToken(payload)
    },
    updateUserInfo(state, payload) {
        state.userInfo = payload
    },
    // 删除token
    removeToken(state) {
        state.token = null // 清除Vuex中的token
        removeToken() // 删除本地缓存的token
    },
}
//请求登录接口拿到token 传到mutations里
const actions = {
    async login({ commit }, obj) {
        const result = await getlogin(obj)
        let results = result.data
        commit("setToken", results)
    },
    // 请求侧边导航数据
    async getprofile({ commit }) {
        const result = await getprofile()
        console.log(31555, result.data.userId);
        const baseInfo = await getUserDetailById(result.data.userId)
        commit('updateUserInfo', { ...result, ...baseInfo }) // 更新到状态中
        return result
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}